﻿using UnityEngine;

public class FoodGrid : Grid
{

    Texture chi = GameManager.gamemanager.chi;
    protected string body;

    protected virtual void subEffect(){}

    public override void Showme()
    {
        GUI.Box(new Rect(ix * isize + iwidth, iy * isize + iwidth, isize, isize), chi);
    }

    public override bool isDead
    {
        get
        {
            return false;
        }
    }

    public void Effect(string _body) 
    {
        body = _body;
        if (body == "1")
        {
            GameManager.gamemanager.score1++;
        }
        else 
        {
            GameManager.gamemanager.score2++;
        }

        subEffect();
    }

}

public class StopFood : FoodGrid
{
    Texture ting = GameManager.gamemanager.ting;
    public override void Showme()
    {
        GUI.Box(new Rect(ix * isize + iwidth, iy * isize + iwidth, isize, isize), ting);
    }

    protected override void subEffect()
    {
        if (body == "1")
        {
            GameManager.gamemanager.myText = "红蛇停止3秒";
        }
        else 
        {
            GameManager.gamemanager.myText = "黄蛇停止3秒";
        }

        GameManager.gamemanager.textTime = 1.0f;
        if (body == "1")
        {
            GameManager.gamemanager.snake2.deltaSec = -3;
        }
        else
        {
            GameManager.gamemanager.snake1.deltaSec = -3;
        }
    }
}

public class LeftFood : FoodGrid
{
    Texture zuo = GameManager.gamemanager.zuo;
    public override void Showme()
    {
        GUI.Box(new Rect(ix * isize + iwidth, iy * isize + iwidth, isize, isize), zuo);
    }
    protected override void subEffect()
    {
        if (body == "1")
        {
            GameManager.gamemanager.myText = "红蛇立即左转";
        }
        else
        {
            GameManager.gamemanager.myText = "黄蛇立即左转";
        }

        GameManager.gamemanager.textTime = 1.0f;
        if (body == "2")
        {
            if (GameManager.gamemanager.snake1.iDirectX == 0 && GameManager.gamemanager.snake1.iDirectY == -1)
            {
                GameManager.gamemanager.snake1.iDirectX = -1;
                GameManager.gamemanager.snake1.iDirectY = 0;
            }
            else if (GameManager.gamemanager.snake1.iDirectX == 0 && GameManager.gamemanager.snake1.iDirectY == 1)
            {
                GameManager.gamemanager.snake1.iDirectX = 1;
                GameManager.gamemanager.snake1.iDirectY = 0;
            }
            else if (GameManager.gamemanager.snake1.iDirectX == -1 && GameManager.gamemanager.snake1.iDirectY == 0)
            {
                GameManager.gamemanager.snake1.iDirectX = 0;
                GameManager.gamemanager.snake1.iDirectY = 1;
            }
            else if (GameManager.gamemanager.snake1.iDirectX == 1 && GameManager.gamemanager.snake1.iDirectY == 0)
            {
                GameManager.gamemanager.snake1.iDirectX = 0;
                GameManager.gamemanager.snake1.iDirectY = -1;
            }
        }
        else 
        {
            if (GameManager.gamemanager.snake2.iDirectX == 0 && GameManager.gamemanager.snake2.iDirectY == -1)
            {
                GameManager.gamemanager.snake2.iDirectX = -1;
                GameManager.gamemanager.snake2.iDirectY = 0;
            }
            else if (GameManager.gamemanager.snake2.iDirectX == 0 && GameManager.gamemanager.snake2.iDirectY == 1)
            {
                GameManager.gamemanager.snake2.iDirectX = 1;
                GameManager.gamemanager.snake2.iDirectY = 0;
            }
            else if (GameManager.gamemanager.snake2.iDirectX == -1 && GameManager.gamemanager.snake2.iDirectY == 0)
            {
                GameManager.gamemanager.snake2.iDirectX = 0;
                GameManager.gamemanager.snake2.iDirectY = 1;
            }
            else if (GameManager.gamemanager.snake2.iDirectX == 1 && GameManager.gamemanager.snake2.iDirectY == 0)
            {
                GameManager.gamemanager.snake2.iDirectX = 0;
                GameManager.gamemanager.snake2.iDirectY = -1;
            }
        }
    }
}

public class RightFood : FoodGrid
{
    Texture you = GameManager.gamemanager.you;
    public override void Showme()
    {
        GUI.Box(new Rect(ix * isize + iwidth, iy * isize + iwidth, isize, isize), you);
    }

    protected override void subEffect()
    {
        if (body == "1")
        {
            GameManager.gamemanager.myText = "红蛇立即右转";
        }
        else
        {
            GameManager.gamemanager.myText = "黄蛇立即右转";
        }
        GameManager.gamemanager.textTime = 1.0f;
        if (body == "2")
        {
            if (GameManager.gamemanager.snake1.iDirectX == 0 && GameManager.gamemanager.snake1.iDirectY == -1)
            {
                GameManager.gamemanager.snake1.iDirectX = 1;
                GameManager.gamemanager.snake1.iDirectY = 0;
            }
            else if (GameManager.gamemanager.snake1.iDirectX == 0 && GameManager.gamemanager.snake1.iDirectY == 1)
            {
                GameManager.gamemanager.snake1.iDirectX = -1;
                GameManager.gamemanager.snake1.iDirectY = 0;
            }
            else if (GameManager.gamemanager.snake1.iDirectX == -1 && GameManager.gamemanager.snake1.iDirectY == 0)
            {
                GameManager.gamemanager.snake1.iDirectX = 0;
                GameManager.gamemanager.snake1.iDirectY = -1;
            }
            else if (GameManager.gamemanager.snake1.iDirectX == 1 && GameManager.gamemanager.snake1.iDirectY == 0)
            {
                GameManager.gamemanager.snake1.iDirectX = 0;
                GameManager.gamemanager.snake1.iDirectY = 1;
            }
        }
        else
        {
            if (GameManager.gamemanager.snake2.iDirectX == 0 && GameManager.gamemanager.snake2.iDirectY == -1)
            {
                GameManager.gamemanager.snake2.iDirectX = 1;
                GameManager.gamemanager.snake2.iDirectY = 0;
            }
            else if (GameManager.gamemanager.snake2.iDirectX == 0 && GameManager.gamemanager.snake2.iDirectY == 1)
            {
                GameManager.gamemanager.snake2.iDirectX = -1;
                GameManager.gamemanager.snake2.iDirectY = 0;
            }
            else if (GameManager.gamemanager.snake2.iDirectX == -1 && GameManager.gamemanager.snake2.iDirectY == 0)
            {
                GameManager.gamemanager.snake2.iDirectX = 0;
                GameManager.gamemanager.snake2.iDirectY = -1;
            }
            else if (GameManager.gamemanager.snake2.iDirectX == 1 && GameManager.gamemanager.snake2.iDirectY == 0)
            {
                GameManager.gamemanager.snake2.iDirectX = 0;
                GameManager.gamemanager.snake2.iDirectY = 1;
            }
        }
    }
}

public class RandomPos : FoodGrid
{
    Texture jian = GameManager.gamemanager.jian;
    public override void Showme()
    {
        GUI.Box(new Rect(ix * isize + iwidth, iy * isize + iwidth, isize, isize), jian);
    }

    protected override void subEffect()
    {
        if (body == "1")
        {
            GameManager.gamemanager.myText = "红蛇身体减少一块";
        }
        else
        {
            GameManager.gamemanager.myText = "黄蛇身体减少一块";
        }
        GameManager.gamemanager.textTime = 1.0f;
        if (body == "2")
        {
            int x = GameManager.gamemanager.lSnakeGrid1[GameManager.gamemanager.lSnakeGrid1.Count - 1].ix;
            int y = GameManager.gamemanager.lSnakeGrid1[GameManager.gamemanager.lSnakeGrid1.Count - 1].iy;
            GameManager.gamemanager.lGrid[x][y] = new EmptyGrid();
            GameManager.gamemanager.lSnakeGrid1.RemoveAt(GameManager.gamemanager.lSnakeGrid1.Count - 1);
        }
        else
        {
            int x = GameManager.gamemanager.lSnakeGrid2[GameManager.gamemanager.lSnakeGrid2.Count - 1].ix;
            int y = GameManager.gamemanager.lSnakeGrid2[GameManager.gamemanager.lSnakeGrid2.Count - 1].iy;
            GameManager.gamemanager.lGrid[x][y] = new EmptyGrid();
            GameManager.gamemanager.lSnakeGrid2.RemoveAt(GameManager.gamemanager.lSnakeGrid2.Count - 1);
        }
    }
}

public class WrongDirector : FoodGrid
{
    Texture luan = GameManager.gamemanager.luan;
    public override void Showme()
    {
        GUI.Box(new Rect(ix * isize + iwidth, iy * isize + iwidth, isize, isize), luan);
    }

    protected override void subEffect()
    {
        if (body == "2")
        {
            GameManager.gamemanager.myText = "黄蛇操作反向";
        }
        else 
        {
            GameManager.gamemanager.myText = "红蛇操作反向";
        }

        GameManager.gamemanager.textTime = 1.0f;
        if (body == "2")
        {
            GameManager.gamemanager.snake1.fxOverTime = Time.time + 5;
            GameManager.gamemanager.dir1 = -1;
        }
        else
        {
            GameManager.gamemanager.snake2.fxOverTime = Time.time + 5;
            GameManager.gamemanager.dir2 = -1;
        }
    }
}

public class EndSwaps : FoodGrid
{
    Texture fan = GameManager.gamemanager.fan;
    public override void Showme()
    {
        GUI.Box(new Rect(ix * isize + iwidth, iy * isize + iwidth, isize, isize), fan);
    }

    protected override void subEffect()
    {
        if (body == "1")
        {
            GameManager.gamemanager.myText = "红蛇首尾互换";
        }
        else
        {
            GameManager.gamemanager.myText = "黄蛇首尾互换";
        }

        GameManager.gamemanager.textTime = 1.0f;
        if (body == "2")
        {
            GameManager.gamemanager.lSnakeGrid1.Reverse();
            GameManager.gamemanager.snake1.iDirectX *= -1;
            GameManager.gamemanager.snake1.iDirectY *= -1;
        }
        else
        {
            GameManager.gamemanager.lSnakeGrid2.Reverse();
            GameManager.gamemanager.snake2.iDirectX *= -1;
            GameManager.gamemanager.snake2.iDirectY *= -1;
        }
    }
}

public class SpeedFood : FoodGrid
{
    Texture shu = GameManager.gamemanager.shu;
    public override void Showme()
    {
        GUI.Box(new Rect(ix * isize + iwidth, iy * isize + iwidth, isize, isize), shu);
    }

    protected override void subEffect()
    {
        if (body == "1")
        {
            GameManager.gamemanager.myText = "红蛇速度降低为初始速度";
        }
        else
        {
            GameManager.gamemanager.myText = "黄蛇速度降低为初始速度";
        }
        GameManager.gamemanager.textTime = 1.0f;
        if (body == "1")
        {
            GameManager.gamemanager.snake2.moveSep = 0.3f;
        }
        else
        {
            GameManager.gamemanager.snake1.moveSep = 0.3f;
        }

    }
}

public class TimeFood : FoodGrid
{
    Texture shi = GameManager.gamemanager.shi;
    public override void Showme()
    {
        GUI.Box(new Rect(ix * isize + iwidth, iy * isize + iwidth, isize, isize), shi);
    }

    protected override void subEffect()
    {
        GameManager.gamemanager.myText = "本局时间减少30秒";
        GameManager.gamemanager.textTime = 1.0f;
        GameManager.gamemanager.gameTimer = GameManager.gamemanager.gameTimer - 30;
    }
}
